package cn.smvc.task.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.smvc.common.utils.StringUtils;
import cn.smvc.task.dao.TTimedTaskDao;
import cn.smvc.task.entity.TTimedTask;
import cn.smvc.task.service.TTimedTaskService;

@Service
public class TTimedTaskServiceImpl implements TTimedTaskService {
	
	@Autowired
	private TTimedTaskDao tTimedTaskDao;

	@Override
	public List<TTimedTask> findAll() throws SQLException {
		return tTimedTaskDao.findAll();
	}
	
	@Override
	public TTimedTask findOne(String id) throws SQLException {
		return tTimedTaskDao.findOne(id);
	}

	@Override
	public int save(TTimedTask task) throws SQLException {
		if(task.getTaskClass().startsWith("http")){
			task.setTaskClassType(1);
		}else{
			task.setTaskClassType(0);
		}
		task.setRollFlag(0);
		String id= task.getId();
		TTimedTask task1 = tTimedTaskDao.findOne(id);
		if(task1 != null && !task1.getId().isEmpty()){
			return tTimedTaskDao.update(task);
		}
		return tTimedTaskDao.save(task);
	}
	
	@Override
	public int delete(String id) throws SQLException {
		return tTimedTaskDao.delete(id);
	}

	@Override
	public int update(TTimedTask task) throws SQLException {
		return tTimedTaskDao.update(task);
	}
	
	@Override
	public int updateNextExecuteDatetime(String nextExecuteTimestamp, String nextExecuteDatetime, String id) 
			throws SQLException {
		return tTimedTaskDao.updateNextExecuteDatetime(nextExecuteTimestamp, nextExecuteDatetime, id);
	}

	@Override
	public int updateNextAndLastExecuteDatetime(String nextExecuteTimestamp, String nextExecuteDatetime,
			String lastExecuteTimestamp, String lastExecuteDatetime, String id) throws SQLException {
		return tTimedTaskDao.updateNextAndLastExecuteDatetime(
				nextExecuteTimestamp, nextExecuteDatetime, 
				lastExecuteTimestamp, lastExecuteDatetime, id);
	}

	@Override
	public int updateAllRunFlag(String runFlag) {
		int rows = 0;
		try {
			rows = tTimedTaskDao.updateAllRunFlag(runFlag);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rows;
	}

	/**
	 * 分页查询
	 */
	@Override
	public Map<String, Object> queryPage(Map<String, String[]> param) {
		int pageNo = Integer.parseInt(param.get("pageNo")[0].toString());
		int pageSize = Integer.parseInt(param.get("pageSize")[0].toString());
		String taskId = param.get("id") == null ? "" : param.get("id")[0];
		String taskName = param.get("taskName") == null ? "" : param.get("taskName")[0];
		String taskGroup = param.get("taskGroup") == null ? "" : param.get("taskGroup")[0];
		String runFlag = param.get("runFlag") == null ? "" : param.get("runFlag")[0];
		String swithFlag = param.get("swithFlag") == null ? "" : param.get("swithFlag")[0];
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		try {
			StringBuilder where = new StringBuilder();
			where.append("1=1 ");
			if(StringUtils.isNotEmpty(taskId)) {
				where.append("and id like'%").append(taskId).append("%' ");
			}
			if(StringUtils.isNotEmpty(taskName)) {
				where.append("and task_name like '%").append(taskName).append("%' ");
			}
			if(StringUtils.isNotEmpty(taskGroup)) {
				where.append("and task_group='").append(taskGroup).append("' ");
			}
			if(StringUtils.isNotEmpty(runFlag)) {
				where.append("and run_flag='").append(runFlag).append("' ");
			}
			if(StringUtils.isNotEmpty(swithFlag)) {
				where.append("and swith_flag='").append(swithFlag).append("' ");
			}
			List<TTimedTask> list = tTimedTaskDao.findPage(where.toString(), pageNo, pageSize, null);
			count = tTimedTaskDao.count(where.toString());
			map.put("code", 0);
			map.put("msg", "查询成功！");
			map.put("data", list);
			map.put("rows", count);//总行数
			map.put("pageNo", pageNo);
			map.put("pageSize", pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
		return map;
	}

	@Override
	public List<TTimedTask> findAllTaskGroup() throws SQLException {
		return tTimedTaskDao.findAllTaskGroup();
	}

	@Override
	public int updateRunFlag(String id, String runFlag) throws SQLException {
		return tTimedTaskDao.updateRunFlag(id, runFlag);
	}

	


}
